<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.js"></script>
    <script src="https://unpkg.com/vuex@next"></script>
    <title>Document</title>
  </head>
  <body>
    <div id="app"></div>
    <script>
      // import {createApp} from 'vue'
      // import {createStore} from 'vuex'
      const { createApp } = Vue;
      const { createStore } = Vuex;

      const myPlugin = (store) => {
        // 当 store 初始化后调用
        store.subscribe((mutation, state) => {
          // 每次 mutation 之后调用
          // mutation 的格式为 { type, payload }
          console.log("mutation:", mutation);
        });
      };

      const store = createStore({
        state: {
          count: 0,
        },
        mutations: {
          increment(state) {
            state.count++;
          },
        },
        plugins: [myPlugin],
      });

      const app = createApp({
        template: `
            <div>{{ $store.state.count }}</div>
            <button @click="$store.commit('increment')">Add</button>
        `,
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>
